<?php

use App\Admin\Extensions\WangEditor;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Column;
use Zhaiduting\ColumnRelation\Relate;

Admin::js('common/hide-footer.js');
$routeName = \Illuminate\Support\Facades\Route::currentRouteName();
// 如果是商品管理就显示属性规格按钮
if($routeName === 'store-goods.index'){
	//dd(request());
	Admin::js('layui/layui.js');
	Admin::css('layui/css/layui.css');
	Admin::disablePjax();
	Admin::js('common/addAttrBtn.js');
}

// 显示秒杀添加商品按钮
if($routeName === 'sharps.index') {
	Admin::js('layui/layui.js');
	Admin::disablePjax();
	Admin::js('common/addSharpGoodsBtn.js');
}

Encore\Admin\Form::forget(['map', 'editor']);
Form::extend('editor', WangEditor::class);
Column::extend('relate', Relate::class);
Column::extend('myqrcode', \App\Admin\Extensions\MyQrcode::class);
Column::extend('mystock', \App\Admin\Extensions\MyStock::class);
//Form::extend('myBelongsToMany', \App\Admin\Extensions\BelongsToMany::class);

Grid::init(function (Grid $grid) use ($routeName){
    $adminRoute = [
        'admin.auth.users.index',
        'admin.auth.roles.index',
        'admin.auth.permissions.index'
    ];
    $grid->expandFilter();
    // 模型表格路由白名单
    $routefillable = [
        'record-goods.index',
        'admin.auth.roles.index',
        'admin.auth.permissions.index',
        'goods-skus.index',
	    'orders.show',
        'admin.handle-selectable',
        'materials.index',
	    'record-materials.create',
	    'record-materials.store',
	    'record-materials.edit',
	    'admin.auth.logs.index'
    ];

    if (!in_array($routeName, $routefillable)) {
        $store_id = empty(Admin::user()->store_id)?'*':Admin::user()->store_id;
        $grid->model()->orderByDesc('created_at')
            ->where('store_id', $store_id);
    }
    $grid->disableExport();

    $grid->actions(function (Grid\Displayers\Actions $actions) use ($grid) {

        $actions->disableView();
    });

    // 管理员去除id筛选增加名称筛选
    if(in_array(\Illuminate\Support\Facades\Route::currentRouteName(), $adminRoute)) {
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('name', '名称');
            $filter->like('http_path', '路由');
        });
    }

});

Form::init(function (Form $form) {
    $form->disableViewCheck();

    $form->tools(function (Form\Tools $tools) {
        $tools->disableView();
    });
});
